home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / buffer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  23.4 KB  |  1,002 lines

  1. /*
  2.  *  linux/fs/buffer.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. /*
  12.  *  'buffer.c' implements the buffer-cache functions. Race-conditions have
  13.  * been avoided by NEVER letting an interrupt change a buffer (except for the
  14.  * data, of course), but instead letting the caller do it.
  15.  */
  16.  
  17. /*
  18.  * NOTE! There is one discordant note here: checking floppies for
  19.  * disk change. This is where it fits best, I think, as it should
  20.  * invalidate changed floppy-disk-caches.
  21.  */
  22.  
  23. #include <stdarg.h>
  24.  
  25. #include <linux/config.h>
  26. #include <linux/errno.h>
  27. #include <linux/sched.h>
  28. #include <linux/kernel.h>
  29. #include <linux/major.h>
  30. #include <linux/string.h>
  31. #include <linux/locks.h>
  32. #include <linux/errno.h>
  33.  
  34. #include <asm/system.h>
  35.  
  36. #ifdef CONFIG_SCSI
  37. #ifdef CONFIG_BLK_DEV_SR
  38. extern int check_cdrom_media_change(int, int);
  39. #endif
  40. #ifdef CONFIG_BLK_DEV_SD
  41. extern int check_scsidisk_media_change(int, int);
  42. extern int revalidate_scsidisk(int, int);
  43. #endif
  44. #endif
  45. #ifdef CONFIG_CDU31A
  46. extern int check_cdu31a_media_change(int, int);
  47. #endif
  48. #ifdef CONFIG_MCD
  49. extern int check_mcd_media_change(int, int);
  50. #endif
  51.  
  52. static int grow_buffers(int pri, int size);
  53.  
  54. static struct buffer_head * hash_table[NR_HASH];
  55. static struct buffer_head * free_list = NULL;
  56. static struct buffer_head * unused_list = NULL;
  57. static struct wait_queue * buffer_wait = NULL;
  58.  
  59. int nr_buffers = 0;
  60. int buffermem = 0;
  61. int nr_buffer_heads = 0;
  62. static int min_free_pages = 20; /* nr free pages needed before buffer grows */
  63. extern int *blksize_size[];
  64.  
  65. /*
  66.  * Rewrote the wait-routines to use the "new" wait-queue functionality,
  67.  * and getting rid of the cli-sti pairs. The wait-queue routines still
  68.  * need cli-sti, but now it's just a couple of 386 instructions or so.
  69.  *
  70.  * Note that the real wait_on_buffer() is an inline function that checks
  71.  * if 'b_wait' is set before calling this, so that the queues aren't set
  72.  * up unnecessarily.
  73.  */
  74. void __wait_on_buffer(struct buffer_head * bh)
  75. {
  76.     struct wait_queue wait = { current, NULL };
  77.  
  78.     bh->b_count++;
  79.     add_wait_queue(&bh->b_wait, &wait);
  80. repeat:
  81.     current->state = TASK_UNINTERRUPTIBLE;
  82.     if (bh->b_lock) {
  83.         schedule();
  84.         goto repeat;
  85.     }
  86.     remove_wait_queue(&bh->b_wait, &wait);
  87.     bh->b_count--;
  88.     current->state = TASK_RUNNING;
  89. }
  90.  
  91. /* Call sync_buffers with wait!=0 to ensure that the call does not
  92.    return until all buffer writes have completed.  Sync() may return
  93.    before the writes have finished; fsync() may not. */
  94.  
  95. static int sync_buffers(dev_t dev, int wait)
  96. {
  97.     int i, retry, pass = 0, err = 0;
  98.     struct buffer_head * bh;
  99.  
  100.     /* One pass for no-wait, three for wait:
  101.        0) write out all dirty, unlocked buffers;
  102.        1) write out all dirty buffers, waiting if locked;
  103.        2) wait for completion by waiting for all buffers to unlock.
  104.      */
  105. repeat:
  106.     retry = 0;
  107.     bh = free_list;
  108.     for (i = nr_buffers*2 ; i-- > 0 ; bh = bh->b_next_free) {
  109.         if (dev && bh->b_dev != dev)
  110.             continue;
  111. #ifdef 0 /* Disable bad-block debugging code */
  112.         if (bh->b_req && !bh->b_lock &&
  113.             !bh->b_dirt && !bh->b_uptodate)
  114.             printk ("Warning (IO error) - orphaned block %08x on %04x\n",
  115.                 bh->b_blocknr, bh->b_dev);
  116. #endif
  117.         if (bh->b_lock)
  118.         {
  119.             /* Buffer is locked; skip it unless wait is
  120.                requested AND pass > 0. */
  121.             if (!wait || !pass) {
  122.                 retry = 1;
  123.                 continue;
  124.             }
  125.             wait_on_buffer (bh);
  126.         }
  127.         /* If an unlocked buffer is not uptodate, there has been
  128.            an IO error. Skip it. */
  129.         if (wait && bh->b_req && !bh->b_lock &&
  130.             !bh->b_dirt && !bh->b_uptodate)
  131.         {
  132.             err = 1;
  133.             continue;
  134.         }
  135.         /* Don't write clean buffers.  Don't write ANY buffers
  136.            on the third pass. */
  137.         if (!bh->b_dirt || pass>=2)
  138.             continue;
  139.         bh->b_count++;
  140.         ll_rw_block(WRITE, 1, &bh);
  141.         bh->b_count--;
  142.         retry = 1;
  143.     }
  144.     /* If we are waiting for the sync to succeed, and if any dirty
  145.        blocks were written, then repeat; on the second pass, only
  146.        wait for buffers being written (do not pass to write any
  147.        more buffers on the second pass). */
  148.     if (wait && retry && ++pass<=2)
  149.         goto repeat;
  150.     return err;
  151. }
  152.  
  153. void sync_dev(dev_t dev)
  154. {
  155.     sync_buffers(dev, 0);
  156.     sync_supers(dev);
  157.     sync_inodes(dev);
  158.     sync_buffers(dev, 0);
  159. }
  160.  
  161. int fsync_dev(dev_t dev)
  162. {
  163.     sync_buffers(dev, 0);
  164.     sync_supers(dev);
  165.     sync_inodes(dev);
  166.     return sync_buffers(dev, 1);
  167. }
  168.  
  169. asmlinkage int sys_sync(void)
  170. {
  171.     sync_dev(0);
  172.     return 0;
  173. }
  174.  
  175. int file_fsync (struct inode *inode, struct file *filp)
  176. {
  177.     return fsync_dev(inode->i_dev);
  178. }
  179.  
  180. asmlinkage int sys_fsync(unsigned int fd)
  181. {
  182.     struct file * file;
  183.     struct inode * inode;
  184.  
  185.     if (fd>=NR_OPEN || !(file=current->filp[fd]) || !(inode=file->f_inode))
  186.         return -EBADF;
  187.     if (!file->f_op || !file->f_op->fsync)
  188.         return -EINVAL;
  189.     if (file->f_op->fsync(inode,file))
  190.         return -EIO;
  191.     return 0;
  192. }
  193.  
  194. void invalidate_buffers(dev_t dev)
  195. {
  196.     int i;
  197.     struct buffer_head * bh;
  198.  
  199.     bh = free_list;
  200.     for (i = nr_buffers*2 ; --i > 0 ; bh = bh->b_next_free) {
  201.         if (bh->b_dev != dev)
  202.             continue;
  203.         wait_on_buffer(bh);
  204.         if (bh->b_dev == dev)
  205.             bh->b_uptodate = bh->b_dirt = bh->b_req = 0;
  206.     }
  207. }
  208.  
  209. /*
  210.  * This routine checks whether a floppy has been changed, and
  211.  * invalidates all buffer-cache-entries in that case. This
  212.  * is a relatively slow routine, so we have to try to minimize using
  213.  * it. Thus it is called only upon a 'mount' or 'open'. This
  214.  * is the best way of combining speed and utility, I think.
  215.  * People changing diskettes in the middle of an operation deserve
  216.  * to loose :-)
  217.  *
  218.  * NOTE! Although currently this is only for floppies, the idea is
  219.  * that any additional removable block-device will use this routine,
  220.  * and that mount/open needn't know that floppies/whatever are
  221.  * special.
  222.  */
  223. void check_disk_change(dev_t dev)
  224. {
  225.     int i;
  226.     struct buffer_head * bh;
  227.  
  228.     switch(MAJOR(dev)){
  229.     case FLOPPY_MAJOR:
  230.         if (!(bh = getblk(dev,0,1024)))
  231.             return;
  232.         i = floppy_change(bh);
  233.         brelse(bh);
  234.         break;
  235.  
  236. #if defined(CONFIG_BLK_DEV_SD) && defined(CONFIG_SCSI)
  237.      case SCSI_DISK_MAJOR:
  238.         i = check_scsidisk_media_change(dev, 0);
  239.         break;
  240. #endif
  241.  
  242. #if defined(CONFIG_BLK_DEV_SR) && defined(CONFIG_SCSI)
  243.      case SCSI_CDROM_MAJOR:
  244.         i = check_cdrom_media_change(dev, 0);
  245.         break;
  246. #endif
  247.  
  248. #if defined(CONFIG_CDU31A)
  249.          case CDU31A_CDROM_MAJOR:
  250.         i = check_cdu31a_media_change(dev, 0);
  251.         break;
  252. #endif
  253.  
  254. #if defined(CONFIG_MCD)
  255.          case MITSUMI_CDROM_MAROR:
  256.         i = check_mcd_media_change(dev, 0);
  257.         break;
  258. #endif
  259.  
  260.      default:
  261.         return;
  262.     };
  263.  
  264.     if (!i) return;
  265.  
  266.     printk("VFS: Disk change detected on device %d/%d\n",
  267.                     MAJOR(dev), MINOR(dev));
  268.     for (i=0 ; i<NR_SUPER ; i++)
  269.         if (super_blocks[i].s_dev == dev)
  270.             put_super(super_blocks[i].s_dev);
  271.     invalidate_inodes(dev);
  272.     invalidate_buffers(dev);
  273.  
  274. #if defined(CONFIG_BLK_DEV_SD) && defined(CONFIG_SCSI)
  275. /* This is trickier for a removable hardisk, because we have to invalidate
  276.    all of the partitions that lie on the disk. */
  277.     if (MAJOR(dev) == SCSI_DISK_MAJOR)
  278.         revalidate_scsidisk(dev, 0);
  279. #endif
  280. }
  281.  
  282. #define _hashfn(dev,block) (((unsigned)(dev^block))%NR_HASH)
  283. #define hash(dev,block) hash_table[_hashfn(dev,block)]
  284.  
  285. static inline void remove_from_hash_queue(struct buffer_head * bh)
  286. {
  287.     if (bh->b_next)
  288.         bh->b_next->b_prev = bh->b_prev;
  289.     if (bh->b_prev)
  290.         bh->b_prev->b_next = bh->b_next;
  291.     if (hash(bh->b_dev,bh->b_blocknr) == bh)
  292.         hash(bh->b_dev,bh->b_blocknr) = bh->b_next;
  293.     bh->b_next = bh->b_prev = NULL;
  294. }
  295.  
  296. static inline void remove_from_free_list(struct buffer_head * bh)
  297. {
  298.     if (!(bh->b_prev_free) || !(bh->b_next_free))
  299.         panic("VFS: Free block list corrupted");
  300.     bh->b_prev_free->b_next_free = bh->b_next_free;
  301.     bh->b_next_free->b_prev_free = bh->b_prev_free;
  302.     if (free_list == bh)
  303.         free_list = bh->b_next_free;
  304.     bh->b_next_free = bh->b_prev_free = NULL;
  305. }
  306.  
  307. static inline void remove_from_queues(struct buffer_head * bh)
  308. {
  309.     remove_from_hash_queue(bh);
  310.     remove_from_free_list(bh);
  311. }
  312.  
  313. static inline void put_first_free(struct buffer_head * bh)
  314. {
  315.     if (!bh || (bh == free_list))
  316.         return;
  317.     remove_from_free_list(bh);
  318. /* add to front of free list */
  319.     bh->b_next_free = free_list;
  320.     bh->b_prev_free = free_list->b_prev_free;
  321.     free_list->b_prev_free->b_next_free = bh;
  322.     free_list->b_prev_free = bh;
  323.     free_list = bh;
  324. }
  325.  
  326. static inline void put_last_free(struct buffer_head * bh)
  327. {
  328.     if (!bh)
  329.         return;
  330.     if (bh == free_list) {
  331.         free_list = bh->b_next_free;
  332.         return;
  333.     }
  334.     remove_from_free_list(bh);
  335. /* add to back of free list */
  336.     bh->b_next_free = free_list;
  337.     bh->b_prev_free = free_list->b_prev_free;
  338.     free_list->b_prev_free->b_next_free = bh;
  339.     free_list->b_prev_free = bh;
  340. }
  341.  
  342. static inline void insert_into_queues(struct buffer_head * bh)
  343. {
  344. /* put at end of free list */
  345.     bh->b_next_free = free_list;
  346.     bh->b_prev_free = free_list->b_prev_free;
  347.     free_list->b_prev_free->b_next_free = bh;
  348.     free_list->b_prev_free = bh;
  349. /* put the buffer in new hash-queue if it has a device */
  350.     bh->b_prev = NULL;
  351.     bh->b_next = NULL;
  352.     if (!bh->b_dev)
  353.         return;
  354.     bh->b_next = hash(bh->b_dev,bh->b_blocknr);
  355.     hash(bh->b_dev,bh->b_blocknr) = bh;
  356.     if (bh->b_next)
  357.         bh->b_next->b_prev = bh;
  358. }
  359.  
  360. static struct buffer_head * find_buffer(dev_t dev, int block, int size)
  361. {
  362.     struct buffer_head * tmp;
  363.  
  364.     for (tmp = hash(dev,block) ; tmp != NULL ; tmp = tmp->b_next)
  365.         if (tmp->b_dev==dev && tmp->b_blocknr==block)
  366.             if (tmp->b_size == size)
  367.                 return tmp;
  368.             else {
  369.                 printk("VFS: Wrong blocksize on device %d/%d\n",
  370.                             MAJOR(dev), MINOR(dev));
  371.                 return NULL;
  372.             }
  373.     return NULL;
  374. }
  375.  
  376. /*
  377.  * Why like this, I hear you say... The reason is race-conditions.
  378.  * As we don't lock buffers (unless we are readint them, that is),
  379.  * something might happen to it while we sleep (ie a read-error
  380.  * will force it bad). This shouldn't really happen currently, but
  381.  * the code is ready.
  382.  */
  383. struct buffer_head * get_hash_table(dev_t dev, int block, int size)
  384. {
  385.     struct buffer_head * bh;
  386.  
  387.     for (;;) {
  388.         if (!(bh=find_buffer(dev,block,size)))
  389.             return NULL;
  390.         bh->b_count++;
  391.         wait_on_buffer(bh);
  392.         if (bh->b_dev == dev && bh->b_blocknr == block && bh->b_size == size)
  393.             return bh;
  394.         bh->b_count--;
  395.     }
  396. }
  397.  
  398. void set_blocksize(dev_t dev, int size)
  399. {
  400.     int i;
  401.     struct buffer_head * bh, *bhnext;
  402.  
  403.     if (!blksize_size[MAJOR(dev)])
  404.         return;
  405.  
  406.     switch(size) {
  407.         default: panic("Invalid blocksize passed to set_blocksize");
  408.         case 512: case 1024: case 2048: case 4096:;
  409.     }
  410.  
  411.     if (blksize_size[MAJOR(dev)][MINOR(dev)] == 0 && size == BLOCK_SIZE) {
  412.         blksize_size[MAJOR(dev)][MINOR(dev)] = size;
  413.         return;
  414.     }
  415.     if (blksize_size[MAJOR(dev)][MINOR(dev)] == size)
  416.         return;
  417.     sync_buffers(dev, 2);
  418.     blksize_size[MAJOR(dev)][MINOR(dev)] = size;
  419.  
  420.   /* We need to be quite careful how we do this - we are moving entries
  421.      around on the free list, and we can get in a loop if we are not careful.*/
  422.  
  423.     bh = free_list;
  424.     for (i = nr_buffers*2 ; --i > 0 ; bh = bhnext) {
  425.         bhnext = bh->b_next_free; 
  426.         if (bh->b_dev != dev)
  427.             continue;
  428.         if (bh->b_size == size)
  429.             continue;
  430.  
  431.         wait_on_buffer(bh);
  432.         if (bh->b_dev == dev && bh->b_size != size)
  433.             bh->b_uptodate = bh->b_dirt = 0;
  434.         remove_from_hash_queue(bh);
  435. /*    put_first_free(bh); */
  436.     }
  437. }
  438.  
  439. /*
  440.  * Ok, this is getblk, and it isn't very clear, again to hinder
  441.  * race-conditions. Most of the code is seldom used, (ie repeating),
  442.  * so it should be much more efficient than it looks.
  443.  *
  444.  * The algoritm is changed: hopefully better, and an elusive bug removed.
  445.  *
  446.  * 14.02.92: changed it to sync dirty buffers a bit: better performance
  447.  * when the filesystem starts to get full of dirty blocks (I hope).
  448.  */
  449. #define BADNESS(bh) (((bh)->b_dirt<<1)+(bh)->b_lock)
  450. struct buffer_head * getblk(dev_t dev, int block, int size)
  451. {
  452.     struct buffer_head * bh, * tmp;
  453.     int buffers;
  454.     static int grow_size = 0;
  455.  
  456. repeat:
  457.     bh = get_hash_table(dev, block, size);
  458.     if (bh) {
  459.         if (bh->b_uptodate && !bh->b_dirt)
  460.             put_last_free(bh);
  461.         return bh;
  462.     }
  463.     grow_size -= size;
  464.     if (nr_free_pages > min_free_pages && grow_size <= 0) {
  465.         if (grow_buffers(GFP_BUFFER, size))
  466.             grow_size = PAGE_SIZE;
  467.     }
  468.     buffers = nr_buffers;
  469.     bh = NULL;
  470.  
  471.     for (tmp = free_list; buffers-- > 0 ; tmp = tmp->b_next_free) {
  472.         if (tmp->b_count || tmp->b_size != size)
  473.             continue;
  474.         if (mem_map[MAP_NR((unsigned long) tmp->b_data)] != 1)
  475.             continue;
  476.         if (!bh || BADNESS(tmp)<BADNESS(bh)) {
  477.             bh = tmp;
  478.             if (!BADNESS(tmp))
  479.                 break;
  480.         }
  481. #if 0
  482.         if (tmp->b_dirt) {
  483.             tmp->b_count++;
  484.             ll_rw_block(WRITEA, 1, &tmp);
  485.             tmp->b_count--;
  486.         }
  487. #endif
  488.     }
  489.  
  490.     if (!bh && nr_free_pages > 5) {
  491.         if (grow_buffers(GFP_BUFFER, size))
  492.             goto repeat;
  493.     }
  494.  
  495. /* and repeat until we find something good */
  496.     if (!bh) {
  497.         if (!grow_buffers(GFP_ATOMIC, size))
  498.             sleep_on(&buffer_wait);
  499.         goto repeat;
  500.     }
  501.     wait_on_buffer(bh);
  502.     if (bh->b_count || bh->b_size != size)
  503.         goto repeat;
  504.     if (bh->b_dirt) {
  505.         sync_buffers(0,0);
  506.         goto repeat;
  507.     }
  508. /* NOTE!! While we slept waiting for this block, somebody else might */
  509. /* already have added "this" block to the cache. check it */
  510.     if (find_buffer(dev,block,size))
  511.         goto repeat;
  512. /* OK, FINALLY we know that this buffer is the only one of its kind, */
  513. /* and that it's unused (b_count=0), unlocked (b_lock=0), and clean */
  514.     bh->b_count=1;
  515.     bh->b_dirt=0;
  516.     bh->b_uptodate=0;
  517.     bh->b_req=0;
  518.     remove_from_queues(bh);
  519.     bh->b_dev=dev;
  520.     bh->b_blocknr=block;
  521.     insert_into_queues(bh);
  522.     return bh;
  523. }
  524.  
  525. void brelse(struct buffer_head * buf)
  526. {
  527.     if (!buf)
  528.         return;
  529.     wait_on_buffer(buf);
  530.     if (buf->b_count) {
  531.         if (--buf->b_count)
  532.             return;
  533.         wake_up(&buffer_wait);
  534.         return;
  535.     }
  536.     printk("VFS: brelse: Trying to free free buffer\n");
  537. }
  538.  
  539. /*
  540.  * bread() reads a specified block and returns the buffer that contains
  541.  * it. It returns NULL if the block was unreadable.
  542.  */
  543. struct buffer_head * bread(dev_t dev, int block, int size)
  544. {
  545.     struct buffer_head * bh;
  546.  
  547.     if (!(bh = getblk(dev, block, size))) {
  548.         printk("VFS: bread: READ error on device %d/%d\n",
  549.                         MAJOR(dev), MINOR(dev));
  550.         return NULL;
  551.     }
  552.     if (bh->b_uptodate)
  553.         return bh;
  554.     ll_rw_block(READ, 1, &bh);
  555.     wait_on_buffer(bh);
  556.     if (bh->b_uptodate)
  557.         return bh;
  558.     brelse(bh);
  559.     return NULL;
  560. }
  561.  
  562. /*
  563.  * Ok, breada can be used as bread, but additionally to mark other
  564.  * blocks for reading as well. End the argument list with a negative
  565.  * number.
  566.  */
  567. struct buffer_head * breada(dev_t dev,int first, ...)
  568. {
  569.     va_list args;
  570.     unsigned int blocksize;
  571.     struct buffer_head * bh, *tmp;
  572.  
  573.     va_start(args,first);
  574.  
  575.     blocksize = BLOCK_SIZE;
  576.     if (blksize_size[MAJOR(dev)] && blksize_size[MAJOR(dev)][MINOR(dev)])
  577.         blocksize = blksize_size[MAJOR(dev)][MINOR(dev)];
  578.  
  579.     if (!(bh = getblk(dev, first, blocksize))) {
  580.         printk("VFS: breada: READ error on device %d/%d\n",
  581.                         MAJOR(dev), MINOR(dev));
  582.         return NULL;
  583.     }
  584.     if (!bh->b_uptodate)
  585.         ll_rw_block(READ, 1, &bh);
  586.     while ((first=va_arg(args,int))>=0) {
  587.         tmp = getblk(dev, first, blocksize);
  588.         if (tmp) {
  589.             if (!tmp->b_uptodate)
  590.                 ll_rw_block(READA, 1, &tmp);
  591.             tmp->b_count--;
  592.         }
  593.     }
  594.     va_end(args);
  595.     wait_on_buffer(bh);
  596.     if (bh->b_uptodate)
  597.         return bh;
  598.     brelse(bh);
  599.     return (NULL);
  600. }
  601.  
  602. /*
  603.  * See fs/inode.c for the weird use of volatile..
  604.  */
  605. static void put_unused_buffer_head(struct buffer_head * bh)
  606. {
  607.     struct wait_queue * wait;
  608.  
  609.     wait = ((volatile struct buffer_head *) bh)->b_wait;
  610.     memset((void *) bh,0,sizeof(*bh));
  611.     ((volatile struct buffer_head *) bh)->b_wait = wait;
  612.     bh->b_next_free = unused_list;
  613.     unused_list = bh;
  614. }
  615.  
  616. static void get_more_buffer_heads(void)
  617. {
  618.     int i;
  619.     struct buffer_head * bh;
  620.  
  621.     if (unused_list)
  622.         return;
  623.  
  624.     if(! (bh = (struct buffer_head*) get_free_page(GFP_BUFFER)))
  625.         return;
  626.  
  627.     for (nr_buffer_heads+=i=PAGE_SIZE/sizeof*bh ; i>0; i--) {
  628.         bh->b_next_free = unused_list;    /* only make link */
  629.         unused_list = bh++;
  630.     }
  631. }
  632.  
  633. static struct buffer_head * get_unused_buffer_head(void)
  634. {
  635.     struct buffer_head * bh;
  636.  
  637.     get_more_buffer_heads();
  638.     if (!unused_list)
  639.         return NULL;
  640.     bh = unused_list;
  641.     unused_list = bh->b_next_free;
  642.     bh->b_next_free = NULL;
  643.     bh->b_data = NULL;
  644.     bh->b_size = 0;
  645.     bh->b_req = 0;
  646.     return bh;
  647. }
  648.  
  649. /*
  650.  * Create the appropriate buffers when given a page for data area and
  651.  * the size of each buffer.. Use the bh->b_this_page linked list to
  652.  * follow the buffers created.    Return NULL if unable to create more
  653.  * buffers.
  654.  */
  655. static struct buffer_head * create_buffers(unsigned long page, unsigned long size)
  656. {
  657.     struct buffer_head *bh, *head;
  658.     unsigned long offset;
  659.  
  660.     head = NULL;
  661.     offset = PAGE_SIZE;
  662.     while ((offset -= size) < PAGE_SIZE) {
  663.         bh = get_unused_buffer_head();
  664.         if (!bh)
  665.             goto no_grow;
  666.         bh->b_this_page = head;
  667.         head = bh;
  668.         bh->b_data = (char *) (page+offset);
  669.         bh->b_size = size;
  670.     }
  671.     return head;
  672. /*
  673.  * In case anything failed, we just free everything we got.
  674.  */
  675. no_grow:
  676.     bh = head;
  677.     while (bh) {
  678.         head = bh;
  679.         bh = bh->b_this_page;
  680.         put_unused_buffer_head(head);
  681.     }
  682.     return NULL;
  683. }
  684.  
  685. static void read_buffers(struct buffer_head * bh[], int nrbuf)
  686. {
  687.     int i;
  688.     int bhnum = 0;
  689.     struct buffer_head * bhr[8];
  690.  
  691.     for (i = 0 ; i < nrbuf ; i++) {
  692.         if (bh[i] && !bh[i]->b_uptodate)
  693.             bhr[bhnum++] = bh[i];
  694.     }
  695.     if (bhnum)
  696.         ll_rw_block(READ, bhnum, bhr);
  697.     for (i = 0 ; i < nrbuf ; i++) {
  698.         if (bh[i]) {
  699.             wait_on_buffer(bh[i]);
  700.         }
  701.     }
  702. }
  703.  
  704. static unsigned long check_aligned(struct buffer_head * first, unsigned long address,
  705.     dev_t dev, int *b, int size)
  706. {
  707.     struct buffer_head * bh[8];
  708.     unsigned long page;
  709.     unsigned long offset;
  710.     int block;
  711.     int nrbuf;
  712.  
  713.     page = (unsigned long) first->b_data;
  714.     if (page & ~PAGE_MASK) {
  715.         brelse(first);
  716.         return 0;
  717.     }
  718.     mem_map[MAP_NR(page)]++;
  719.     bh[0] = first;
  720.     nrbuf = 1;
  721.     for (offset = size ; offset < PAGE_SIZE ; offset += size) {
  722.         block = *++b;
  723.         if (!block)
  724.             goto no_go;
  725.         first = get_hash_table(dev, block, size);
  726.         if (!first)
  727.             goto no_go;
  728.         bh[nrbuf++] = first;
  729.         if (page+offset != (unsigned long) first->b_data)
  730.             goto no_go;
  731.     }
  732.     read_buffers(bh,nrbuf);         /* make sure they are actually read correctly */
  733.     while (nrbuf-- > 0)
  734.         brelse(bh[nrbuf]);
  735.     free_page(address);
  736.     ++current->min_flt;
  737.     return page;
  738. no_go:
  739.     while (nrbuf-- > 0)
  740.         brelse(bh[nrbuf]);
  741.     free_page(page);
  742.     return 0;
  743. }
  744.  
  745. static unsigned long try_to_load_aligned(unsigned long address,
  746.     dev_t dev, int b[], int size)
  747. {
  748.     struct buffer_head * bh, * tmp, * arr[8];
  749.     unsigned long offset;
  750.     int * p;
  751.     int block;
  752.  
  753.     bh = create_buffers(address, size);
  754.     if (!bh)
  755.         return 0;
  756.     p = b;
  757.     for (offset = 0 ; offset < PAGE_SIZE ; offset += size) {
  758.         block = *(p++);
  759.         if (!block)
  760.             goto not_aligned;
  761.         tmp = get_hash_table(dev, block, size);
  762.         if (tmp) {
  763.             brelse(tmp);
  764.             goto not_aligned;
  765.         }
  766.     }
  767.     tmp = bh;
  768.     p = b;
  769.     block = 0;
  770.     while (1) {
  771.         arr[block++] = bh;
  772.         bh->b_count = 1;
  773.         bh->b_dirt = 0;
  774.         bh->b_uptodate = 0;
  775.         bh->b_dev = dev;
  776.         bh->b_blocknr = *(p++);
  777.         nr_buffers++;
  778.         insert_into_queues(bh);
  779.         if (bh->b_this_page)
  780.             bh = bh->b_this_page;
  781.         else
  782.             break;
  783.     }
  784.     buffermem += PAGE_SIZE;
  785.     bh->b_this_page = tmp;
  786.     mem_map[MAP_NR(address)]++;
  787.     read_buffers(arr,block);
  788.     while (block-- > 0)
  789.         brelse(arr[block]);
  790.     ++current->maj_flt;
  791.     return address;
  792. not_aligned:
  793.     while ((tmp = bh) != NULL) {
  794.         bh = bh->b_this_page;
  795.         put_unused_buffer_head(tmp);
  796.     }
  797.     return 0;
  798. }
  799.  
  800. /*
  801.  * Try-to-share-buffers tries to minimize memory use by trying to keep
  802.  * both code pages and the buffer area in the same page. This is done by
  803.  * (a) checking if the buffers are already aligned correctly in memory and
  804.  * (b) if none of the buffer heads are in memory at all, trying to load
  805.  * them into memory the way we want them.
  806.  *
  807.  * This doesn't guarantee that the memory is shared, but should under most
  808.  * circumstances work very well indeed (ie >90% sharing of code pages on
  809.  * demand-loadable executables).
  810.  */
  811. static inline unsigned long try_to_share_buffers(unsigned long address,
  812.     dev_t dev, int *b, int size)
  813. {
  814.     struct buffer_head * bh;
  815.     int block;
  816.  
  817.     block = b[0];
  818.     if (!block)
  819.         return 0;
  820.     bh = get_hash_table(dev, block, size);
  821.     if (bh)
  822.         return check_aligned(bh, address, dev, b, size);
  823.     return try_to_load_aligned(address, dev, b, size);
  824. }
  825.  
  826. #define COPYBLK(size,from,to) memcpy((void *)to,(const void *)from,size)
  827.  
  828. /*
  829.  * bread_page reads four buffers into memory at the desired address. It's
  830.  * a function of its own, as there is some speed to be got by reading them
  831.  * all at the same time, not waiting for one to be read, and then another
  832.  * etc. This also allows us to optimize memory usage by sharing code pages
  833.  * and filesystem buffers..
  834.  */
  835. unsigned long bread_page(unsigned long address, dev_t dev, int b[], int size, int prot)
  836. {
  837.     struct buffer_head * bh[8];
  838.     unsigned long where;
  839.     int i, j;
  840.  
  841.     if (!PAGE_IS_RW(prot)) {
  842.         where = try_to_share_buffers(address,dev,b,size);
  843.         if (where)
  844.             return where;
  845.     }
  846.     ++current->maj_flt;
  847.      for (i=0, j=0; j<PAGE_SIZE ; i++, j+= size) {
  848.         bh[i] = NULL;
  849.         if (b[i])
  850.             bh[i] = getblk(dev, b[i], size);
  851.     }
  852.     read_buffers(bh,i);
  853.     where = address;
  854.      for (i=0, j=0; j<PAGE_SIZE ; i++, j += size,address += size) {
  855.         if (bh[i]) {
  856.             if (bh[i]->b_uptodate)
  857.                 COPYBLK(size, (unsigned long) bh[i]->b_data,address);
  858.             brelse(bh[i]);
  859.         }
  860.     }
  861.     return where;
  862. }
  863.  
  864. /*
  865.  * Try to increase the number of buffers available: the size argument
  866.  * is used to determine what kind of buffers we want.
  867.  */
  868. static int grow_buffers(int pri, int size)
  869. {
  870.     unsigned long page;
  871.     struct buffer_head *bh, *tmp;
  872.  
  873.     if ((size & 511) || (size > PAGE_SIZE)) {
  874.         printk("VFS: grow_buffers: size = %d\n",size);
  875.         return 0;
  876.     }
  877.     if(!(page = __get_free_page(pri)))
  878.         return 0;
  879.     bh = create_buffers(page, size);
  880.     if (!bh) {
  881.         free_page(page);
  882.         return 0;
  883.     }
  884.     tmp = bh;
  885.     while (1) {
  886.         if (free_list) {
  887.             tmp->b_next_free = free_list;
  888.             tmp->b_prev_free = free_list->b_prev_free;
  889.             free_list->b_prev_free->b_next_free = tmp;
  890.             free_list->b_prev_free = tmp;
  891.         } else {
  892.             tmp->b_prev_free = tmp;
  893.             tmp->b_next_free = tmp;
  894.         }
  895.         free_list = tmp;
  896.         ++nr_buffers;
  897.         if (tmp->b_this_page)
  898.             tmp = tmp->b_this_page;
  899.         else
  900.             break;
  901.     }
  902.     tmp->b_this_page = bh;
  903.     buffermem += PAGE_SIZE;
  904.     return 1;
  905. }
  906.  
  907. /*
  908.  * try_to_free() checks if all the buffers on this particular page
  909.  * are unused, and free's the page if so.
  910.  */
  911. static int try_to_free(struct buffer_head * bh, struct buffer_head ** bhp)
  912. {
  913.     unsigned long page;
  914.     struct buffer_head * tmp, * p;
  915.  
  916.     *bhp = bh;
  917.     page = (unsigned long) bh->b_data;
  918.     page &= PAGE_MASK;
  919.     tmp = bh;
  920.     do {
  921.         if (!tmp)
  922.             return 0;
  923.         if (tmp->b_count || tmp->b_dirt || tmp->b_lock)
  924.             return 0;
  925.         tmp = tmp->b_this_page;
  926.     } while (tmp != bh);
  927.     tmp = bh;
  928.     do {
  929.         p = tmp;
  930.         tmp = tmp->b_this_page;
  931.         nr_buffers--;
  932.         if (p == *bhp)
  933.             *bhp = p->b_prev_free;
  934.         remove_from_queues(p);
  935.         put_unused_buffer_head(p);
  936.     } while (tmp != bh);
  937.     buffermem -= PAGE_SIZE;
  938.     free_page(page);
  939.     return !mem_map[MAP_NR(page)];
  940. }
  941.  
  942. /*
  943.  * Try to free up some pages by shrinking the buffer-cache
  944.  *
  945.  * Priority tells the routine how hard to try to shrink the
  946.  * buffers: 3 means "don't bother too much", while a value
  947.  * of 0 means "we'd better get some free pages now".
  948.  */
  949. int shrink_buffers(unsigned int priority)
  950. {
  951.     struct buffer_head *bh;
  952.     int i;
  953.  
  954.     if (priority < 2)
  955.         sync_buffers(0,0);
  956.     bh = free_list;
  957.     i = nr_buffers >> priority;
  958.     for ( ; i-- > 0 ; bh = bh->b_next_free) {
  959.         if (bh->b_count || !bh->b_this_page)
  960.             continue;
  961.         if (bh->b_lock)
  962.             if (priority)
  963.                 continue;
  964.             else
  965.                 wait_on_buffer(bh);
  966.         if (bh->b_dirt) {
  967.             bh->b_count++;
  968.             ll_rw_block(WRITEA, 1, &bh);
  969.             bh->b_count--;
  970.             continue;
  971.         }
  972.         if (try_to_free(bh, &bh))
  973.             return 1;
  974.     }
  975.     return 0;
  976. }
  977.  
  978. /*
  979.  * This initializes the initial buffer free list.  nr_buffers is set
  980.  * to one less the actual number of buffers, as a sop to backwards
  981.  * compatibility --- the old code did this (I think unintentionally,
  982.  * but I'm not sure), and programs in the ps package expect it.
  983.  *                    - TYT 8/30/92
  984.  */
  985. void buffer_init(void)
  986. {
  987.     int i;
  988.     extern ulong high_memory;
  989.  
  990.     if (high_memory >= 4*1024*1024)
  991.         min_free_pages = 200;
  992.     else
  993.         min_free_pages = 20;
  994.     for (i = 0 ; i < NR_HASH ; i++)
  995.         hash_table[i] = NULL;
  996.     free_list = 0;
  997.     grow_buffers(GFP_KERNEL, BLOCK_SIZE);
  998.     if (!free_list)
  999.         panic("VFS: Unable to initialize buffer free list!");
  1000.     return;
  1001. }
  1002.